<%--
  Created by IntelliJ IDEA.
  User: 21933
  Date: 2021/1/20
  Time: 17:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js"></script>
    <style>
        h3{
            background: #00FF00;
        }
    </style>
</head>
<body>
<div>
    <h3>待审核表单</h3>
</div>
<%--table表 待审核表--%>
    <table class="layui-hide" id="allApply" lay-filter="allApply"></table>
<div>
    <h3>审核记录</h3>
</div>
    <table class="layui-hide" id="auditHistory" lay-filter="auditHistory"></table>
<%--头部工具栏--%>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="createOutTable">同意</button>
        </div>
    </script>
<%--右侧操作栏--%>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="agree">同意</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="disagree">不同意</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="allInfo">查看详情</a>
    </script>

<%--审核记录表的右侧工具栏--%>
    <script type="text/html" id="historyBarDemo">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="allAuditInfo">查看详情</a>
    </script>

</body>
<script>
    layui.use(['table','jquery','layer'], function(){
        var table = layui.table;
        var $=layui.jquery;
        var layer=layui.layer;
        //页面加载完成后执行的事情
        $(function () {
            allApply();
            auditHistory();
        })


        //查询书本类型库
        function getType(){
            $.ajax({
                type:'get',
                url:'${pageContext.request.contextPath}/store/getType',
                dataType:'json',
                success:function (typedata) {
                    for(var i=0;i<typedata.length;i++){
                        $("#typeSelect").append(
                            $("<option>").attr("value",typedata[i][0]).html(typedata[i][1])
                        )
                    }
                }
            })
        }
        //生成待审核表
        function allApply(){
            table.render({
                elem: '#allApply'
                ,url:'${pageContext.request.contextPath}/store/showNoAudit'
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,title: '书本库存'
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'id', title:'ID',fixed: 'left',hide:true,unresize: true, sort: true}
                    ,{field:'outnumber', title:'出库单编号'}
                    ,{field:'staffid', title:'申请人id',width:100}
                    ,{field:'staffname', title:'申请人姓名',width:100}
                    ,{field:'bookcount', title:'总量(本)',sort: true,width:120}
                    ,{field:'bookprice', title:'总价(元)',sort: true,width:80}
                    ,{field:'createdate', title:'申请时间'}
                    ,{field:'status', title:'审核状态',width:120,templet:function (d) {
                                return "<button class='layui-btn layui-btn-xs layui-btn-danger'>待审核</button>";
                        }}
                   // ,{field:'disagree', title:'审核意见',edit:true}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
                ]]
                ,page: true
                ,limit:5 //设置一页几条
                ,limits:[5,10,15] //放置可供
            });
        }

        //详情表
        function detailInfo(outnumber){
            alert(outnumber);
            table.render({
                elem: '#detailInfo'
                ,url:'${pageContext.request.contextPath}/detail/showInfoBOUT?outnumber='+outnumber
                ,title: '订单详情'
                ,cols: [[
                    // {type: 'checkbox', fixed: 'left'}
                    {field:'id', title:'ID',fixed: 'left',hide:true,unresize: true, sort: true}
                    ,{field:'out_outnumber', title:'出库单编号'}
                    ,{field:'bookname', title:'书名',width:100}
                    ,{field:'price', title:'进价',width:100}
                    ,{field:'bookcount', title:'总量(本)',sort: true,width:120}
                    ,{field:'count', title:'库存数量'}
                    ,{field:'publisher', title:'出版社',width:120}
                    // ,{field:'disagree', title:'审核意见',edit:true}
                    // ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
                ]]
                ,page: true
                ,limit:5 //设置一页几条
                ,limits:[5,10,15] //放置可供
            });
        }

        //审核记录表
        function auditHistory(){
            table.render({
                elem: '#auditHistory'
                ,url:'${pageContext.request.contextPath}/audit/allAudit'
                ,title: '审核记录表'
                ,cols: [[
                    ,{field:'id', title:'ID',fixed: 'left',hide:true,unresize: true, sort: true}
                    ,{field:'outauditnumber', title:'审核单编号'}
                    ,{field:'out_outnumber', title:'申请单编号',sort: true,width:120}
                    ,{field:'audit_result', title:'审核结果',width:120}
                    ,{field:'audit_info', title:'审核意见',width:120}
                    ,{field:'audit_date', title:'审核时间'}
                    ,{fixed: 'right', title:'操作', toolbar: '#historyBarDemo'}
                ]]
                ,page: true
                ,limit:5 //设置一页几条
                ,limits:[5,10,15] //放置可供
            });
        }

        //搜索框
        $("#reloads").click(function () {
            //获得作者的写入框
            var author=$("#topAuthor");
            //获得类型的选择框
            var bookType=$("#typeSelect");
            //执行重载
            table.reload('allBook', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    author: author.val(),
                    typeid:bookType.val()
                }
            });
        })

        // 监听表单的头工具栏事件
        table.on('toolbar(outApply)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'outApply':
                    var outDatas = checkStatus.data;
                    $.ajax({
                        type:'post',
                        url:'${pageContext.request.contextPath}/out',
                        data:JSON.stringify(outDatas),
                        contentType:'application/json',
                        dataType: 'json',
                        success:function (datasu) {
                            if(datasu.code==1){
                                layer.closeAll();
                                allbook();
                                layer.msg(datasu.msg);
                            }else {
                                layer.msg(datasu.msg);
                            }
                        }
                    })
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //头工具栏事件
        table.on('toolbar(allBook)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'createOutTable':
                    break;
                case 'addstaff':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听审核记录表的行工具事件
        table.on('tool(auditHistory)', function(obj){
            var data=obj.data;
            //alert("shenhe");
            alert(data.out_outnumber);
            if(obj.event==='allAuditInfo'){
                layer.open({
                    type: 1,
                    content: '<table class="layui-hide" id="detailInfo" lay-filter="detailInfo"></table>', //这里content是一个普通的String
                    success:function () {
                        detailInfo(data.out_outnumber);
                    }
                });
            }
        });


        //监听行工具事件
        table.on('tool(allApply)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'agree'){
                // alert(JSON.stringify(data));
                $.ajax({
                    type:'post',
                    url:'${pageContext.request.contextPath}/store/agree',
                    data:JSON.stringify(data),
                    contentType: 'application/json',
                    dataType:'json',
                    success:function (data) {
                        if (data.code==1){
                            allApply();
                            layer.msg(data.msg,{icon:1});
                        }else {
                            layer.msg(data.msg,{icon:5});
                        }
                    }
                })
            } else if(obj.event === 'disagree'){
                var disagreeinfo;
                layer.prompt({
                    formType: 2,
                    value: '原因.....',
                    title: '请输入不同意原因',
                    area: ['200px', '150px'] //自定义文本域宽高
                }, function(value, index, elem){
                    disagreeinfo=value; //得到value
                    layer.close(index);
                    alert(disagreeinfo);
                    // alert(JSON.stringify(data));
                    console.log(data);
                    $.ajax({
                        type:'post',
                        url:'${pageContext.request.contextPath}/audit/Outdisagree',
                        data: {
                            "disagree":disagreeinfo,
                            "applyData":JSON.stringify(data)
                        },
                        //contentType:'application/json',
                        dataType:'json',
                        success:function (returnmsg) {
                            if (returnmsg.code==1){
                                allApply();
                                layer.msg(returnmsg.msg,{icon:1});
                            }else {
                                layer.msg(returnmsg.msg,{icon:5});
                            }
                        }
                    })

                });
            }else if(obj.event === 'allInfo'){
            //        查看详情
            //     alert(data.outnumber);
                layer.open({
                    type: 1,
                    content: '<table class="layui-hide" id="detailInfo" lay-filter="detailInfo"></table>', //这里content是一个普通的String
                    success:function () {
                        detailInfo(data.outnumber)
                    }
                });
            }
        });
    });
</script>
</html>
